import javax.swing.*;

import java.util.ArrayList;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

class SavedEvent extends EventObject {
	public String name;
	public SavedEvent(Object source) {
		super(source);
	}
	public void setName(String name) {
		this.name = name;
	}
}

// our listener interface
// we extend EventListener because it makes the Java gods happy
interface SavedListener extends EventListener {
	void saved(SavedEvent e);
}

// An interface implemented by the objects our listener wants to listent to
interface SavedEventGenertor {
	void addSavedListener(SavedListener listener);
}


public class SettingsWindow extends JFrame implements ActionListener, SavedEventGenertor {

	private static final long serialVersionUID = 1L;
	
	private ArrayList<SavedListener> savedListeners = 
		new ArrayList<SavedListener>();

	
	Bridget bridget;
	Professions professions;
	
	private JPanel mainPanel;
	private JTextField weightField;
	private JTextField heightField;
	private JTextField ageField;
	private JComboBox sexBox;
	private JComboBox professionBox;
	private JComboBox activityBox;
	private JButton saveButton;
	
	
	
	public SettingsWindow(Bridget bridget) {
		super("Personal settings");
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		
		this.bridget = bridget;
		professions = new Professions();
		
		JLabel label;
		
		mainPanel = new JPanel();
		//mainPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));
		
		weightField = new JTextField("0");
		label = new JLabel("Weight:", JLabel.LEFT);
		label.setAlignmentX(LEFT_ALIGNMENT);
		label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		mainPanel.add(label);
		//mainPanel.setAlignmentX(LEFT_ALIGNMENT);
		mainPanel.add(weightField);
		
		heightField = new JTextField("0");
		label = new JLabel("Height:", JLabel.LEFT);
		label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		mainPanel.add(label);
		mainPanel.add(heightField);
		
		ageField = new JTextField("0");
		label = new JLabel("Age:", JLabel.LEFT);
		label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		mainPanel.add(label);
		mainPanel.add(ageField);
		
		String[] sexs = { "female", "male" };
		sexBox = new JComboBox(sexs);
		label = new JLabel("Sex:", JLabel.LEFT);
		label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		mainPanel.add(label);
		mainPanel.add(sexBox);
		
		ArrayList<String> profList = professions.getAllProfessions();
		String[] profs = new String[profList.size()];
		for (int i = 0; i < profList.size(); i++) {
			profs[i] = profList.get(i);
		}
		professionBox = new JComboBox(profs);
		label = new JLabel("Profession:", JLabel.LEFT);
		label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		mainPanel.add(label);
		mainPanel.add(professionBox);
		
		String[] activity = { "Every day",
				              "Five times a week",
				              "Tree times a week",
				              "One time a week",
				              "More seldom" };
		activityBox = new JComboBox(activity);
		label = new JLabel("I go in for sport:");
		label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		mainPanel.add(label);
		mainPanel.add(activityBox);
				            
		
		//mainPanel.add(Box.createVerticalGlue());
		
		saveButton = new JButton("Save data");
		saveButton.setActionCommand("save clicked");
		saveButton.addActionListener(this);
		//saveButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		mainPanel.add(saveButton);
		
		updateFields();
		
		JPanel window = new JPanel();
		window.setLayout(new BorderLayout());
		window.add(mainPanel, BorderLayout.NORTH);
		window.add(saveButton, BorderLayout.SOUTH);
		window.setPreferredSize(new Dimension(350, 400));
		
		setContentPane(window);
		pack();
		
	}
	
	public void updateFields() {
		weightField.setText(String.valueOf(bridget.getWeight()));
		heightField.setText(String.valueOf(bridget.getHeight()));
		ageField.setText(String.valueOf(bridget.getAge()));
		
		/* set sex */
		if (bridget.getSex()) {
			sexBox.setSelectedItem(0);
		} else {
			sexBox.setSelectedItem(1);
		}
		
		professionBox.setSelectedItem(bridget.getProfession());
		activityBox.setSelectedIndex((int)bridget.getFactor());
		
	}

	public void actionPerformed(ActionEvent e) {
		if ("save clicked".equals(e.getActionCommand())) {
			
			double newWeight = Double.parseDouble(weightField.getText()); 
			if ( newWeight != bridget.getWeight() ) {
				bridget.setWeight(newWeight);
			}
			
			double newHeight = Double.parseDouble(heightField.getText()); 
			if ( newHeight != bridget.getHeight() ) {
				bridget.setHeight(newHeight);
			}
			
			int newAge = Integer.parseInt(ageField.getText()); 
			if ( newAge != bridget.getAge() ) {
				bridget.setAge(newAge);
			}
			
		    boolean newSex = sexBox.getSelectedItem().toString().equals("female"); 
			if ( newSex != bridget.getSex() ) {
				bridget.setSex(newSex);
			}
			
			String newProfession = professionBox.getSelectedItem().toString();
			if (!(newProfession.equals(bridget.getProfession()))) {
				bridget.setProfession(newProfession);
			}
			
			int newActivity = activityBox.getSelectedIndex();
			if (newActivity != (int)bridget.getFactor()) {
				bridget.setFactor1(newActivity);
			}
			
			SavedEvent event = new SavedEvent(this);
			event.name = "settings";
			this.fireSavedEvent(event);
			

			
		} 
		
	}
	
	private void fireSavedEvent(SavedEvent e) {
		int j = savedListeners.size();
		if (j==0) { return; }
		for(int i=0; i<j; i++) {
			savedListeners.get(i).saved(e);
		}
	}

	public void addSavedListener(SavedListener listener) {
		savedListeners.add(listener);
		
	}

}
